package com.huaxin.common.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * A Simple pull refersh view
 * Created by hebing on 2016/7/1.
 */
public class RefreshLinearLayout extends LinearLayout {

    Refreshable proxy;

    public RefreshLinearLayout(Context context) {
        this(context, null);
    }

    public RefreshLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public RefreshLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        proxy = new Refreshable(this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        proxy.init();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //here we can always get down y
        proxy.process(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return proxy.process(ev);
    }

    public void setListener(Refreshable.RefreshListener listener) {
        proxy.setListener(listener);
    }

    public void onRefreshFinish() {
        proxy.onRefreshFinish();
    }

}
